<template>
  <view>
    <button @click="downloadAndPreviewImage">下载并预览图片</button>
  </view>
</template>

<script>
export default {
  methods: {
    downloadAndPreviewImage() {
      const imageUrl = "https://cdn.pixabay.com/photo/2025/11/05/20/57/monastery-9939590_1280.jpg";
      const imageTask = uni.downloadFile({
        url: imageUrl,
        success: (res) => {
          if (res.statusCode === 200) {
            console.log('下载成功');
            uni.previewImage({
              urls: [res.tempFilePath]
            });
          }
        },
        fail: (err) => {
          console.error('下载失败:', err);
        }
      });

      // 监听下载进度
      imageTask.onProgressUpdate((res) => {
        console.log('下载进度:', res.progress + "%"); // 进度百分比
        console.log('已下载:', res.totalBytesWritten);
        console.log('总大小:', res.totalBytesExpectedToWrite);
      });
    }
  }
}
</script>